package com.wxr.controller;


import com.sun.java.swing.plaf.windows.WindowsTextAreaUI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JdbcController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    //查询数据库的所有信息
    @GetMapping("/deptList")
    public List<Map<String,Object>> userList(){

        String sql = " select * from  dept ";

        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);

        return maps;
    }

    @GetMapping("/addDept")
    public String addDept(){
        String sql = " insert into dept( dname,db_source ) values('测试部',DATABASE())  ";
        jdbcTemplate.update(sql);
        return "add ok";
    }

    @GetMapping("/updateDept/{deptNo}")
    public String updateDept(@PathVariable("deptNo") Integer deptNo){
        String sql = " update dept set dname=? where deptno="+deptNo;
        Object[] objects = new Object[1];
        objects[0] = "测试部1";
        jdbcTemplate.update(sql,objects);
        return "update ok";
    }

    @GetMapping("/deleteDept/{deptNo}")
    public String deleteDept(@PathVariable("deptNo")Integer deptNo){
        String sql = " delete from dept where deptNo=?";
        jdbcTemplate.update(sql,deptNo);
        return "delete ok";
    }

}
